package com.toogu.mazhangmen.ui.main;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.view.View;
import android.widget.AdapterView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.bean.SockReturnBean;
import com.toogu.mazhangmen.service.SockService;
import com.toogu.mazhangmen.ui.user.UserActivity;
import com.toogu.mazhangmen.ui.view.RadioGroupView;
import com.toogu.mazhangmen.ui.view.SearchView;
import com.toogu.mazhangmen.util.EquipmentUtil;
import com.toogu.mazhangmen.util.OkhttpUtil;
import com.toogu.mazhangmen.util.ToastUtil;

import java.util.List;
import java.util.Queue;

import static android.content.Context.BIND_AUTO_CREATE;

public class MainPresenter implements MainContract.IPresenter{
    private MainContract.IView mainView = null;
    private MainContract.IModel mainModel = null;
    private Activity activity= null;
    private SockService.SockBinder mBinder;

    public MainPresenter(Activity activity){
        this.activity=activity;
    }

    @Override
    public void onCreate(MainContract.IView view, MainContract.IModel model) {
        this.mainView = view;
        this.mainModel = model;
        view.setViewDelegate(IViewDelegate);
        model.setModelDelegate(IModelDelegate);
        view.onCreate();
        model.onCreate();
    }
    @Override
    public void onResume() {
        ToastUtil.getToastUtil().show(activity);//开启消息提醒
        activity.bindService(new Intent(activity, SockService.class),serviceConnection, BIND_AUTO_CREATE);//连接服务
        mainView.onResume();
        mainModel.onResume();
    }
    @Override
    public void onPause() {
        ToastUtil.getToastUtil().close();//关闭消息提醒
        if(mBinder!=null) {//关闭服务链接
            mBinder.onUnbind(1);
            activity.unbindService(serviceConnection);
            mBinder=null;
        }
        mainView.onPause();
        mainModel.onPause();
    }
    @Override
    public void onDestory() {
        mainView.onDestory();
        mainModel.onDestory();
        OkhttpUtil.getOkhttpUtil().cancelTag(activity);

    }

    //服务连接
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBinder = (SockService.SockBinder) service;
            mBinder.getHelp(new SockService.OnProgressListener() {
                @Override
                public void onSockCallbackProgress(Queue<SockReturnBean> queue) {
                    while (!queue.isEmpty()) {
                        SockReturnBean sockReturnBean = queue.poll();
                        EquipmentsBean equipmentsBean = EquipmentUtil.setHelp(sockReturnBean);
                        activity.runOnUiThread(()->mainView.notify(equipmentsBean));
                    }
                }
            });
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    private final MainContract.IView.Delegate IViewDelegate = new MainContract.IView.Delegate() {
        private int last = 0;

        //点击事件
        @Override
        public View.OnClickListener getViewOnClickListener() {
            return new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.startActivity(new Intent(activity, UserActivity.class));
                }
            };
        }
        //SearchView的搜索
        @Override
        public SearchView.OnClickListener getSearchViewOnClickListener() {
            return new SearchView.OnClickListener() {
                @Override
                public void onSearchViewClick(String string) {
                    last=-1;
                    mainView.showWaiting();
                    mainView.onSearch();
                    mainModel.getEquipment(EquipmentUtil.Search,string);
                }
            };
        }
        //左上角三个按钮的点击
        @Override
        public RadioGroupView.OnCheckedChangeListener getRadioGroupViewOnCheckedChangeListener() {
            return new RadioGroupView.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(int situation) {
                    mainView.onRadioGroup();
                    switch (situation){
                        case 0:
                            if(last==0) return;
                            last=0;
                            mainView.showWaiting();
                            mainModel.getEquipment(EquipmentUtil.Door,null);
                            break;
                        case 1:
                            if(last==1) return;
                            last=1;
                            mainView.showWaiting();
                            mainModel.getEquipment(EquipmentUtil.Help,null);
                            break;
                        case 2:
                            if(last==2) return;
                            last=2;
                            mainView.showWaiting();
                            mainModel.getEquipment(EquipmentUtil.OutLine,null);
                            break;
                    }
                }
            };
        }
        //多选的选择
        @Override
        public AdapterView.OnItemSelectedListener getAdapterViewOnItemSelectedListener() {
            return new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    last=-1;
                    if(position==0) return;
                    mainView.showWaiting();
                    mainView.onSpinner(position);
                    mainModel.getEquipment(position,null);
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            };
        }
        //刷新
        @Override
        public OnRefreshLoadMoreListener getOnRefreshLoadMoreListener() {
            return new OnRefreshLoadMoreListener() {
                @Override//加载更多
                public void onLoadMore(RefreshLayout refreshLayout) {

                }
                @Override//刷新
                public void onRefresh(RefreshLayout refreshLayout) {
                    last=-1;
                    mainView.onCreate();
                    mainModel.getEquipment(EquipmentUtil.Door,null);
                }
            };
        }
    };

    private final MainContract.IModel.Delegate IModelDelegate = new MainContract.IModel.Delegate() {
        @Override
        public void onDataChanged(List<EquipmentsBean> equipmentsBeanList) {
            mainView.showNewViewPager(equipmentsBeanList);
        }

        @Override
        public void onSetProjectList(String[] ProjectName) {
            mainView.setSpinnerDate(ProjectName);
        }

        @Override
        public void onHideWaiting() {
            mainView.hideWaiting();
        }

    };


}
